package day17.feifei.wangyunfei;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class Demo05 {
    public static void main(String[] args) {
        int totalPrice = calculateTotalPrice();
        System.out.println("所有顾客订单的总价格为：" + totalPrice);
    }
    public static int calculateTotalPrice() {
        int totalPrice = 0;
        HashMap<String, Integer> dishPrices = new HashMap<>();
        try (BufferedReader dishesReader = new BufferedReader(new FileReader("D:\\file\\io\\dishes.txt"));
             BufferedReader ordersReader = new BufferedReader(new FileReader("D:\\file\\io\\orders.txt"))) {
            // 从dishes.txt读取菜品及其价格
            String line;
            while ((line = dishesReader.readLine()) != null) {
                String[] dishInfo = line.split("：");
                String dish = dishInfo[0];
                int price = Integer.parseInt(dishInfo[1]);
                dishPrices.put(dish, price);
            }
            // 从orders.txt计算所有顾客订单的总价格
            while ((line = ordersReader.readLine()) != null) {
                String[] orderItems = line.split("，");
                int orderTotal = 0;
                for (String item : orderItems) {
                    String[] dishInfo = item.split("：");
                    String dish = dishInfo[0];
                    int quantity = Integer.parseInt(dishInfo[1]);
                    int price = dishPrices.getOrDefault(dish, 0);
                    orderTotal += price * quantity;
                }
                totalPrice += orderTotal;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return totalPrice;
    }
}
